package org.example.review.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.lettuce.core.dynamic.annotation.Param;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.example.review.dto.WorkDTO;

import java.util.List;

@Mapper
public interface WorkMapper extends BaseMapper<com.common.pojo.entity.Apply> {

    /**
     * 查询工作列表
     * @return 工作列表
     */
    @Select("SELECT " +
            "a.id, " +
            "CONCAT('出差申请-', COALESCE(a.destination, '未知目的地')) as title, " +
            "CONCAT('用户', a.user_id) as applicant, " +
            "COALESCE(SUM(p.budget_amount), 0) / 100.0 as amount, " +
            "a.start_time as date, " +
            "CASE " +
            "  WHEN a.status IN (2, 3) THEN 'high' " +
            "  WHEN a.status IN (6, 7) THEN 'medium' " +
            "  ELSE 'low' " +
            "END as priority, " +
            "CASE " +
            "  WHEN a.status = 2 THEN 'pending' " +
            "  WHEN a.status IN (4, 8) THEN 'approved' " +
            "  WHEN a.status IN (1, 5) THEN 'rejected' " +
            "  ELSE 'pending' " +
            "END as status, " +
            "'travel' as type, " +
            "COALESCE(a.purpose, '出差申请') as description " +
            "FROM apply a " +
            "LEFT JOIN plan p ON a.apply_id = p.apply_id " +
            "GROUP BY a.id, a.destination, a.user_id, a.start_time, a.status, a.purpose " +
            "ORDER BY a.create_time DESC")
    List<WorkDTO> selectWorkList();

    /**
     * 审核通过工作项 - 根据当前状态更新到下一个状态
     * @param id 工作项ID
     * @return 影响的行数
     */
    /**
     * 审核通过工作项（无备注）
     * 支持：2,3,1 -> 4 (差旅/预支相关) 和 6,7,5 -> 8 (报销/支付相关)
     * @param id 工作项ID
     * @return 影响的行数
     */
    @Update("UPDATE apply SET " +
            "status = CASE " +
            "  WHEN status IN (2, 3, 1) THEN 4 " +
	            "  WHEN status IN (6, 7, 5) THEN 8 " +
            "  ELSE status " +
            "END, " +
            "update_time = NOW() " +
            "WHERE id = #{id} AND status IN (1, 2, 3, 5, 6, 7)")
    int approveWork(Integer id);
    /**
     * 审核通过工作项（带备注）
     * 支持：2,3,1 -> 4 (差旅/预支相关) 和 6,7,5 -> 8 (报销/支付相关)
     * @param id 工作项ID
     * @param remark 操作备注
     * @return 影响的行数
     */
    @Update("UPDATE apply SET " +// 【修复】更新备注
            "status = CASE " +
            "  WHEN status IN (2, 3, 1) THEN 4 " +
	            "  WHEN status IN (6, 7, 5) THEN 8 " +
            "  ELSE status " +
            "END, " +
            "update_time = NOW() " +
            "WHERE id = #{id} AND status IN (1, 2, 3, 5, 6, 7)")
    int approveWorkWithRemark(Integer id, String remark);
    /**
     * 审核驳回工作项（无备注）
     * 支持：2,3,4 -> 1 (差旅/预支相关) 和 6,7,8 -> 5 (报销/支付相关)
     * @param id 工作项ID
     * @return 影响的行数
     */
    @Update("UPDATE apply SET " +
            "status = CASE " +
            "  WHEN status IN (2, 3, 4) THEN 1 " +
	            "  WHEN status IN (6, 7, 8) THEN 5 " +
            "  ELSE status " +
            "END, " +
            "update_time = NOW() " +
            "WHERE id = #{id} AND status IN (2, 3, 4, 6, 7, 8)")
    int rejectWork(Integer id);
    /**
     * 审核驳回工作项（带备注）
     * 支持：2,3,4 -> 1 (差旅/预支相关) 和 6,7,8 -> 5 (报销/支付相关)
     * @param id 工作项ID
     * @param remark 驳回原因/备注
     * @return 影响的行数
     */
    @Update("UPDATE apply SET " +// 【修复】更新备注
            "status = CASE " +
            "  WHEN status IN (2, 3, 4) THEN 1 " +
	            "  WHEN status IN (6, 7, 8) THEN 5 " +
            "  ELSE status " +
            "END, " +
            "update_time = NOW() " +
            "WHERE id = #{id} AND status IN (2, 3, 4, 6, 7, 8)")
    int rejectWorkWithRemark(Integer id, String remark);

    /**
     * 获取工作项当前状态
     * @param id 工作项ID
     * @return 当前状态
     */
    @Select("SELECT status FROM apply WHERE id = #{id}")
    Integer selectCurrentStatus(Integer id);

}